﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FytSoa.Common;
using FytSoa.Core.Model.Sw;
using FytSoa.Extensions;
using FytSoa.Service.DtoModel;
using FytSoa.Service.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FytSoa.Api.Controllers.Shop
{
    [Route("api/shop/[controller]")]
    [ApiController]
    [JwtAuthorize(Roles = "Admin")]
    public class BrandController : ControllerBase
    {
        private readonly ISw_brandService _brandService;
        private readonly ISw_typesService _typeService;
        public BrandController(ISw_brandService brandService
            , ISw_typesService typeService)
        {
            _brandService = brandService;
            _typeService = typeService;
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet("list")]
        public async Task<IActionResult> GetBrandPage([FromQuery]PageParm param)
        {
            return Ok(await _brandService.GetPagesAsync(param, m => true, m => m.Sort, Common.DbOrderEnum.Desc));
        }

        /// <summary>
        /// 根据id获得信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("getmodel")]
        public async Task<IActionResult> GetModel([FromBody]ParmString obj)
        {
            var typeModel = await _brandService.GetModelAsync(m => m.Guid == obj.parm);
            var typeList =await _typeService.GetListAsync();
            var resDto = new SwBrandDto()
            {
                model = typeModel.data,
                list = typeList.data
            };
            return Ok(new ApiResult<SwBrandDto>()
            {
                data = resDto,
                statusCode = typeModel.statusCode,
                message = typeModel.message
            });
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<IActionResult> Add([FromBody]Sw_brand model)
        {
            model.Guid = Guid.NewGuid().ToString();
            if (!string.IsNullOrEmpty(model.NickName))
            {
                model.Letter = model.NickName.Substring(0, 1).ToUpper();
            }
            return Ok(await _brandService.AddAsync(model));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("update")]
        public async Task<IActionResult> Update([FromBody]Sw_brand model)
        {
            if (!string.IsNullOrEmpty(model.NickName))
            {
                model.Letter = model.NickName.Substring(0, 1).ToUpper();
            }
            return Ok(await _brandService.UpdateAsync(model));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        public async Task<IActionResult> Delete([FromBody]ParmString obj)
        {
            return Ok(await _brandService.DeleteAsync(obj.parm));
        }
    }
}